#! /usr/bin/env python3

#Author: Ahmed Abdulshafy
#Time: Monday, 29 July 2013, 9:30 AM
#Last Modification: Tuesday, 13 August 2013, 11:37 PM

import cdr_db_mysql, math
	
def rate(cdr):
	fcd = cdr["fcd"]
	ocg = cdr["ocg"]
	time_con = cdr["t_con"]
	duration = cdr["dur"]

	db = cdr_db_mysql.cdrMySQL()
	dest_ptrn_id = db.match_pattern(fcd)
	orig_ptrn_id = db.match_pattern(ocg)
	time_range_id = db.match_time_range(time_con)

	return math.ceil(duration/60.0) * db.rate_rd(dest_ptrn_id, orig_ptrn_id, time_range_id)


if __name__ == "__main__":
	rt = rate({"ocg": '4457', "fcd": '22362407', "t_orig":35364777878, "t_con": 458764687, "dur": 100})
	#cdr_wr({"ocg": 4457, "fcd": 7090, "t_orig":35364777878, "t_con": 458764687, "dur": 0, "money": rt})
	#print(cdr_rd())
	print rt